<?php

if (!class_exists("QPlex"))
{
    require_once (CORE_DIR . '/qxcore/qplex.php');
}

abstract class View
{
    public $ViewPath;
    public $VarsArray = array();
    protected $viewName;
    private $blocks = array();

    function __construct($name = '', $vars = array(), $path = '')
    {
        if (! empty($name))
        {
            $this->viewName = strtolower($name);
        }

        if ($vars && is_array($vars))
        {
            $this->VarsArray = $vars;
        }

        if(!empty ($path))
        {            
            $this->ViewPath = $path;
        }

        $this->VarsArray['web_url'] = WEB_URL;
    }

    function __toString()
    {
        return $this->content();
    }

    public static function Load($name, $vars = array(), $path = '')
    {        
        return new QView($name, $vars, $path);
    }

    public function Render()
    {        
        echo $this->Content();
    }

    public function Content()
    {
        $content = (string)$this->loadView();

        return $content;
    }

    public function Set($array)
    {
        if (is_array($array) && count($array) > 0)
        {
            $this->VarsArray = $array;
        }

        return $this;
    }

    public function SetName($name)
    {
        if (! empty($name))
        {
            $this->viewName = strtolower($name);
        }
    }

    public function GetName()
    {
        return empty($this->viewName) ? false : $this->viewName;
    }

    public function SetVar($name, $value)
    {
        if (! empty($name))
        {
            $this->VarsArray[$name] = $value;
        }
    }

    public function GetVar($name)
    {
        if (! empty($name))
        {
            return $this->VarsArray[$name];
        }
    }

    public function SetBlock($name, $view)
    {
        $this->blocks[$name] = $view;
    }

    public function Script($name)
    {
        QXC()->setGlobal(null, $name, "SCRIPTS");

        return $this;
    }

    public function Style($name)
    {
        QXC()->setGlobal(null, $name, "STYLES");

        return $this;
    }

    public function Message($text, $stick = false)
    {
        QXC()->setGlobal(null, $text, "MESSAGES", true);

        return $this;
    }

    private function loadView()
    {
        $viewFile = "{$this->viewName}." . CORE_VIEW_EXT;
        $paths = (array)QXC()->Config->views;

        if($modulePath = QXC::getGlobal("ModulePath", "QXC"))
        {
            array_unshift($paths, "{$modulePath}/views");
        }
        else if($this->ViewPath)
        {
            array_unshift($paths, $this->ViewPath);
        }
        
        if($path = QXC::pathExist($paths, $viewFile))
        {            
            $qp = new QPlex($path, $this->VarsArray);
            $qp->view = $this;

            if($this->blocks)
            {
                $qp->SetBlock($this->blocks, $this->ViewPath);
            }
            
            return $qp->Prepare();
        }
        else
        {
            throw new QException("{$viewFile} view not found");
        }

        return false;
    }
}

class QView extends View
{
/**
 * Magically set template data.
 *
 * @param string Key of the data
 * @param string Value of the data
 */
    function __set($name, $value)
    {
        $this->{$name} = $value;
        $this->SetVar($name, $value);
    }

    /**
     * Magically gets a template variable.
     *
     * @param string $key
     * @return mixed
     */
    function __get($name)
    {
        return $this->GetVar($name);
    }
}
